package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Company;
import com.itheima.service.store.CompanyService;
import com.itheima.service.store.impl.CompanyServiceImpl;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: mm
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-13 11:02
 **/
//uri:/store/company?operation=list
@WebServlet("/store/company")
public class CompanyServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("/store/company");
        String operation = request.getParameter("operation");

        if ("list".equals(operation)) {
            list(request, response);
        } else if ("toAdd".equals(operation)) {
            toAdd(request, response);
        } else if ("save".equals(operation)) {
            save(request, response);
        } else if ("toEdit".equals(operation)) {
            toEdit(request, response);
        } else if ("edit".equals(operation)) {
            edit(request, response);
        } else if ("delete".equals(operation)) {
            delete(request, response);
        }

    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Company company = BeanUtil.fillBean(request, Company.class, "yyyy-DD-mm");

        // CompanyService companyService = new CompanyServiceImpl();
        companyService.delete(company);
        //跳转到页面list
        response.sendRedirect(request.getContextPath() + "/store/company?operation=list");

    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Company company = BeanUtil.fillBean(request, Company.class, "yyyy-DD-mm");
        // CompanyService companyService = new CompanyServiceImpl();
        companyService.update(company);
        System.out.println(request.getRequestURI());
        System.out.println(request.getContextPath());
        System.out.println(company);
        //跳转到页面list
        response.sendRedirect(request.getContextPath() + "/store/company?operation=list");

    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从数据库中查到数据,渲染到页面上
        String id = request.getParameter("id");
        // CompanyService companyService = new CompanyServiceImpl();
        Company company = companyService.findById(id);
        request.setAttribute("company", company);
        request.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp")
                .forward(request, response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Company company = BeanUtil.fillBean(request, Company.class, "yyyy-DD-mm");
        // CompanyService companyService = new CompanyServiceImpl();
        companyService.save(company);
        System.out.println(request.getRequestURI());
        System.out.println(request.getContextPath());
        System.out.println(company);
        //跳转到页面list
        response.sendRedirect(request.getContextPath() + "/store/company?operation=list");
    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(request, response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据
        CompanyService companyService = new CompanyServiceImpl();
        int page = 1;
        int size = 5;
        String pageStr = request.getParameter("page");
        String sizeStr = request.getParameter("size");
        if (StringUtils.isNotBlank(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(sizeStr)) {
            size = Integer.parseInt(sizeStr);
        }
        PageInfo all = companyService.findAll(page, size);
        request.setAttribute("page", all);
        //就跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp")
                .forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
